<template>
  <div class="infoTable-containner">
    <div class="tableSearch">
      <el-form :inline="true"  class="demo-form-inline"  size="mini" >
        <el-form-item label="接收人:">
          <el-input class="shortPadding" v-model="searchName"></el-input>
        </el-form-item>
        <el-form-item label="状态:">
          <el-select class="shortPadding" v-model="value5">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发送时间:" style="margin-right: 0px">
          <el-date-picker
            v-model="model.starttime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="startTime"
            placeholder="开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item class="endTime" label="~" style="margin-right: 5px">
          <el-date-picker
            v-model="model.endtime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="endTime"
            placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-right: 6px">
          <el-button type="primary" @click="this.timeSearch" >查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="this.buttonClean">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pagination-container footerPage">
      <el-pagination
        @current-change="CurrentChange"
        @size-change="SizeChange"
        :page-sizes="[5, 10, 20, 50]"
        background layout="total, sizes, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
    <div  class="tableList">
      <el-table
        :data="tableData"
        border
        stripe border fit highlight-current-row
        style="width: 100%; max-height:617px; overflow-y: auto "
        :height='617'
        :show-overflow-tooltip="true"
        :header-cell-style="{background:'#D8DEE8'}"
      >
        <el-table-column
          prop="index"
          label="序号"
          align="center"
          width="60px"
        >
        </el-table-column>
        <el-table-column
          v-for="(column, index) in cols"
          :prop="column.prop"
          :label="column.label"
          :key="index"
          :width="column.width"
          :sortable="column.sortable"
        >
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
  import axios from 'axios'

  export default {
    data() {
      return {
        // 验证时间逻辑
        startTime: {
          disabledDate: time => {
            if (this.endtime) {
              return (
                time.getTime() > new Date(this.endtime).getTime()
              )
            } else {
              return time.getTime() > Date.now()
            }
          }
        },
        endTime: {
          disabledDate: time => {
            if (this.model.starttime) {
              return (
                time.getTime() > Date.now() ||
                time.getTime() < new Date(this.model.starttime).getTime()
              )
            } else {
              return time.getTime() > Date.now()
            }
          }
        },
        // 时间
        model: {
          starttime: '',
          endtime: ''
        },
        // 搜索 {
        searchName: '',
        // 下拉
        options: [],
        // 下拉框
        value5: '',
        // }
        cols: [
          { prop: 'send_time', label: '发送时间', width: '190px', sortable: true },
          { prop: 'station', label: '事件源', width: '390px', sortable: true },
          { prop: 'content', label: '事件描述' },
          { prop: 'user_name', label: '接收人', width: '270px', sortable: true },
          { prop: 'send_status', label: '状态', width: '180px', sortable: true }
        ],
        tableData: [],
        indexMethod: '',
        total: 0,
        // 换页
        siteListQuery: {
          page: 1,
          limit: 20,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        // 告警发送记录接口
        submitdata: {
          access_token: localStorage.getItem('access_token'),
          type: 'send_record_get_condition',
          data: {
            condition: {
              begin_time: '',
              end_time: '',
              tos: '',
              send_type: '',
              send_status: '',
              send_time: '',
              content: '',
              event_level: '',
              event_type: '',
              equip_name: '',
              station: '',
              user_id: '',
              user_name: ''
            },
            page: {
              page_size: '10',
              page_number: '1'
            }
          }
        },
        selectTable: {
          access_token: localStorage.getItem('access_token'),
          type: 'send_record_get_status'
        }
      }
    },
    mounted() {
      // this.alarmRecordData()
      // this.selectData()
    },
    methods: {
      // 调取时间方法
      drawLine() {
        var strMouth = new Date().getMonth() + 1
        if (strMouth > 0 && strMouth < 10) {
          strMouth = '0' + strMouth
        }
        var strDate = new Date().getDate()
        if (strDate > 0 && strDate < 10) {
          strDate = '0' + strDate
        }
        var strMinutes = new Date().getMinutes()
        if (strMinutes > 0 && strMinutes < 10) {
          strMinutes = '0' + strMinutes
        }
        var strSeconds = new Date().getSeconds()
        if (strSeconds > 0 && strSeconds < 10) {
          strSeconds = '0' + strSeconds
        }
        this.model.starttime = (new Date().getFullYear() + '-' + strMouth + '-' + strDate + ' ' + '00' + ':' + '00' + ':' + '00')
        this.model.endtime = (new Date().getFullYear() + '-' + strMouth + '-' + strDate + ' ' + new Date().getHours() + ':' + strMinutes + ':' + strSeconds)
        this.submitdata.data.condition.begin_time = this.model.starttime
        this.submitdata.data.condition.end_time = this.model.endtime
      },
      // 警告
      alertMassage() {
        if ((this.submitdata.data.condition.begin_time === this.model.starttime && this.submitdata.data.condition.begin_time !== '') || (this.submitdata.data.condition.end_time === this.model.endtime && this.submitdata.data.condition.end_time !== '')) {
          this.alarmRecordData()
        } else {
          this.$message({
            type: 'info',
            message: '请选择查询时间段，时间是查询的必须条件'
          })
        }
      },
      // 换页
      handleSizeChange(val) {
        this.siteListQuery.limit = val
        this.gettableData()
      },
      handleCurrentChange(val) {
        this.siteListQuery.page = val
        this.tableData()
      },
      // 查询条件
      timeSearch() {
        this.submitdata.data.condition.begin_time = this.model.starttime
        this.submitdata.data.condition.end_time = this.model.endtime
        this.submitdata.data.condition.send_status = this.value5
        this.submitdata.data.condition.user_name = this.searchName
        this.alertMassage()
      },
      buttonClean() {
        this.model.starttime = ''
        this.model.endtime = ''
        this.value5 = ''
        this.searchName = ''
        this.alarmRecordData()
        console.log(this.submitdata.data.condition.begin_time)
      },
      alarmRecordData() {
        /* 有登录后可调整为以下代码**/
        /*
        updateRecord(this.submitdata).then(res => {
          console.log(11111111111111)
          console.log(res)
          this.$router.push({
            // path: 'usermanage'
          })
        }).catch(() => {
          console.log('失败!')
          return
        })*/
        // 告警总览
        axios.post(process.env.SERVER_URL_ALARM_SERVICE + '/sender', this.submitdata, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.tableData = res.data.data.items
          for (let a = 0; a < res.data.data.items.length; a++) {
            this.tableData[a].index = (a + 1) + (res.data.data.page.page_number - 1) * res.data.data.page.page_size
          }
          this.total = parseInt(res.data.data.page.total)
          // console.log(this.tableData)
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
            if (error.data.errmsg === 'BeginTime error:example:[  2006-01-02 15:04:05  ]') {
              this.$message({
                type: 'info',
                message: '请选择开始时间'
              })
            } else if (error.data.errmsg === 'EndTime error:example:[  2006-01-02 15:04:05  ]') {
              this.$message({
                type: 'info',
                message: '请选择结束时间'
              })
            }
          })
      },
      // 下拉框接口获取
      selectData() {
        axios.post(process.env.SERVER_URL_ALARM_SERVICE + '/sender', this.selectTable, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.options = res.data.datas
          console.log(res)
          console.log(this.options)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 当前页
      CurrentChange(val) {
        this.submitdata.data.page.page_number = (val) + ''
        this.alarmRecordData()
      },
      // 每页显示多少条
      SizeChange(pageSize) {
        this.submitdata.data.page.page_size = (pageSize) + ''
        this.alarmRecordData()
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .infoTable-containner{
    width: 100%;
    min-height: 500px;
    .tableSearch{
      width: 100%;
      max-height: 50px;
      background-color: #F0F3F5;
      font-size:14px;
      font-family:MicrosoftYaHei;
      font-weight:400;
      color:rgba(105,114,126,1);
      line-height: 48px;
      padding: 0 20px  0 20px;
      .el-input {
        position: relative;
        font-size: 14px;
        display: inline-block;
        width: 100%;
        /*padding-top: 10px;*/
      }
    }
    .tableList{
      width: 100%;
      min-height:20vw;
    }
    .footerPage{
      float: right;
      right:-9px;
      position: relative;
    }
  }
  /deep/.el-table--striped .el-table__body tr.el-table__row--striped td {
    background: #FAFAFA;
  }
  /deep/ .el-form--inline .el-form-item__content {
    display: inline-block;
    vertical-align: middle;
  }
  /deep/ .el-form--inline .el-form-item {
    margin-right: 25px;
  }
  /deep/ .el-table--border th, .el-table__fixed-right-patch{
    padding: 3px 0;
  }
  /deep/ .endTime > label.el-form-item__label {
    padding-right: 4px;
  }
  /deep/ .shortPadding + .el-input__inner{
    padding: 0px 25px 0 10px;
  }
  /deep/ .el-pagination .el-select .el-input .el-input__inner{
    height: 24px;
  }
</style>
